package com.wms.web.utils;

import com.wms.base.domain.BaseMat;
import com.wms.base.domain.BaseMatBom;
import com.wms.base.service.BaseMatBomService;
import com.wms.base.service.BaseMatService;
import com.wms.common.bean.MatBomTree;
import com.wms.common.utils.bean.BeanUtils;
import com.wms.stock.domain.StockOutDetail;
import com.wms.system.service.SysDictDataService;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class MatBomTreeUtils {
    private static final String dictType = "base_mat_unit";

    public static List<StockOutDetail> getStockOutDetailList(List<BaseMatBom> baseMatBomList, BaseMatBomService baseMatBomService, BaseMatService baseMatService, SysDictDataService sysDictDataService) {
        List<StockOutDetail> resultList = new ArrayList<>();
        List<MatBomTree> matBomTreeList = getTreeList(baseMatBomList, baseMatBomService);
        if (CollectionUtils.isNotEmpty(matBomTreeList)) {
            StockOutDetail detail = null;
            for (MatBomTree matBomTree : matBomTreeList) {
                detail = new StockOutDetail();
//                物料编码
                String matCode = matBomTree.getChildMatCode();
                detail.setMatCode(matCode);
//                物料名称
                BaseMat baseMat = baseMatService.selectBaseMatByMatCode(matCode);
                detail.setMatName(baseMat != null ? baseMat.getMatName() : "");
//                财务编码
                detail.setFdCode(baseMat.getFdCode());
//                图号
                detail.setFigNum(baseMat.getFigNum());
//                物料组
                detail.setMatGroup(baseMat.getMatGroup());
//                物料分类
                detail.setMatClass(baseMat.getMatClass());
//                单位
                detail.setUnitCode(baseMat.getUnitCode());
                detail.setUnitName(sysDictDataService.selectDictLabel(dictType, detail.getUnitCode()));
//                数量
                detail.setQuantity(matBomTree.getChildMatNum());
                resultList.add(detail);
            }
        }
        return resultList;
    }

    public static List<MatBomTree> getTreeList(List<BaseMatBom> baseMatBomList, BaseMatBomService baseMatBomService) {
        List<MatBomTree> resultList = new ArrayList<>();
        MatBomTree matBomTree = null;
        for (BaseMatBom item : baseMatBomList) {
            matBomTree = new MatBomTree();
            BeanUtils.copyBeanProp(matBomTree, item);
            if ("Y".equals(item.getIsFictitious())) {
                matBomTree.setChildren(iterateChildren(resultList, item.getChildMatCode(), baseMatBomService));
            } else {
                checkList(resultList, matBomTree);
            }
        }
        return resultList;
    }

    /**
     * 递归子信息
     */
    private static List<MatBomTree> iterateChildren(List<MatBomTree> matBomTreeList, String fatherMatCode, BaseMatBomService baseMatBomService) {
        List<MatBomTree> resultList = new ArrayList<>();
        List<BaseMatBom> bomList = baseMatBomService.selectBaseMatBomByFatherMatCode(fatherMatCode);
        MatBomTree matBomTree = null;
        for (BaseMatBom item : bomList) {
            matBomTree = new MatBomTree();
            BeanUtils.copyBeanProp(matBomTree, item);
            if ("Y".equals(item.getIsFictitious())) {
                matBomTree.setChildren(iterateChildren(matBomTreeList, item.getChildMatCode(), baseMatBomService));
            } else {
                checkList(resultList, matBomTree);
            }
            resultList.add(matBomTree);
        }
        return resultList;
    }

    private static void checkList(List<MatBomTree> matBomTreeList, MatBomTree matBomTree) {
        boolean check = false;
        String childMatCode = matBomTree.getChildMatCode();
        for (MatBomTree item : matBomTreeList) {
            if (childMatCode.equals(item.getChildMatCode())) {
                item.setChildMatNum(item.getChildMatNum().add(matBomTree.getChildMatNum()));
                check = true;
                break;
            }
        }
        if (!check) {
            matBomTreeList.add(matBomTree);
        }
    }
}
